<?php
/*
 * 打赏周期
 */
namespace app\admin\controller;
use app\common\controller\AdminBase;
use app\common\model\Cycle as CycleModel;
use think\facade\Db;

class Cycle extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
    }
    public function index()
    {
        
    }
    //打赏周期
    public function lists()
    {
        $param = $this->request->param();
        if (!empty($param['title'])) {
            $where = function ($query) use ($param){$query->where('name','LIKE', '%'.trim($param['title']).'%');};
        }else{
           $where = 1;
        }
        $list = CycleModel::where($where)->order('id','DESC')->paginate();
        return $this->fetch('index',['list'=>$list]);
    }
    //添加
    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证规则
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'cycle');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $result = CycleModel::create($param);
            !$result && $this->error($this->errorMsg);
            insert_admin_log('添加了打赏周期');
            $this->success('添加成功');
        }
        return $this->fetch('save');
    }
    //编辑
    public function edit()
    {
    	if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证规则
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'cycle');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $result = CycleModel::update($param);
            !$result && $this->error($this->errorMsg);
            insert_admin_log('修改了打赏周期');
            $this->success('修改成功');
        }
        return $this->fetch('save', ['data' => Db::table('cycle')->find(input('id'))]);
    }
    //删除
    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            CycleModel::destroy($param['id']);
            insert_admin_log('删除了打赏周期');
            $this->success('删除成功');
        }
    }

}